package com.vegetable.modules.controller.finance;

import com.vegetable.common.utils.R;
import com.vegetable.modules.finance.service.ChinaUMSWFService;
import com.vegetable.modules.finance.entity.wf.QueryTransRequest;
import com.vegetable.modules.finance.entity.wf.QuerySettleDetailRequest;
import com.vegetable.modules.finance.entity.wf.QueryTransSummaryRequest;
import com.vegetable.modules.finance.entity.wf.AccountSummaryRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 银联商务网服-实时交易查询控制器
 * 提供网服相关接口的HTTP访问入口
 */
@Slf4j
@RestController
@RequestMapping("/chinaums/wf")
@Api(tags = "银联商务网服-实时交易查询接口")
public class ChinaUMSWFController {

    @Autowired
    private ChinaUMSWFService chinaUMSWFService;

    /**
     * 实时交易查询接口
     * @param request 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    @PostMapping("/queryTrans")
    @ApiOperation("实时交易查询接口")
    public R queryTrans(@RequestBody QueryTransRequest request,
                        @RequestHeader("Authorization") String authorization,
                        @RequestParam(defaultValue = "false") boolean isProduction) {
        try {
            Map<String, String> params = new HashMap<>();
            if (request.getGroupCode() != null) {
                params.put("groupCode", request.getGroupCode());
            }
            if (request.getMchntNo() != null) {
                params.put("mchntNo", request.getMchntNo());
            }
            params.put("queryType", request.getQueryType());
            params.put("appld", request.getAppld());
            params.put("transBeginTime", request.getTransBeginTime());
            params.put("transEndTime", request.getTransEndTime());
            if (request.getTermNo() != null) {
                params.put("termNo", request.getTermNo());
            }
            if (request.getMchntOrderld() != null) {
                params.put("mchntOrderld", request.getMchntOrderld());
            }
            if (request.getRefNo() != null) {
                params.put("refNo", request.getRefNo());
            }
            params.put("pageldx", request.getPageldx());
            if (request.getPageSize() != null) {
                params.put("pageSize", request.getPageSize());
            }
            if (request.getBelongBranch() != null) {
                params.put("belongBranch", request.getBelongBranch());
            }
            
            Map<String, Object> result = chinaUMSWFService.queryTrans(params, authorization, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("实时交易查询接口调用失败", e);
            return R.fail("实时交易查询接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 对账明细查询接口
     * @param request 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    @PostMapping("/querySettleDetail")
    @ApiOperation("对账明细查询接口")
    public R querySettleDetail(@RequestBody QuerySettleDetailRequest request,
                               @RequestHeader("Authorization") String authorization,
                               @RequestParam(defaultValue = "false") boolean isProduction) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("beginSettDate", request.getBeginSettDate());
            params.put("endSettDate", request.getEndSettDate());
            if (request.getBeginTransDate() != null) {
                params.put("beginTransDate", request.getBeginTransDate());
            }
            if (request.getEndTransDate() != null) {
                params.put("endTransDate", request.getEndTransDate());
            }
            params.put("mchntNo", request.getMchntNo());
            params.put("busiType", request.getBusiType());
            if (request.getTransChnl() != null) {
                params.put("transChnl", request.getTransChnl());
            }
            if (request.getTermNo() != null) {
                params.put("termNo", request.getTermNo());
            }
            if (request.getRefld() != null) {
                params.put("refld", request.getRefld());
            }
            if (request.getMchntOrderld() != null) {
                params.put("mchntOrderld", request.getMchntOrderld());
            }
            if (request.getUmsOrderld() != null) {
                params.put("umsOrderld", request.getUmsOrderld());
            }
            params.put("pageldx", request.getPageldx());
            if (request.getPageSize() != null) {
                params.put("pageSize", request.getPageSize());
            }
            if (request.getGroupCode() != null) {
                params.put("groupCode", request.getGroupCode());
            }
            params.put("queryType", request.getQueryType());
            
            Map<String, Object> result = chinaUMSWFService.querySettleDetail(params, authorization, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("对账明细查询接口调用失败", e);
            return R.fail("对账明细查询接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 实时交易汇总查询接口
     * @param request 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    @PostMapping("/queryTransSummary")
    @ApiOperation("实时交易汇总查询接口")
    public R queryTransSummary(@RequestBody QueryTransSummaryRequest request,
                               @RequestHeader("Authorization") String authorization,
                               @RequestParam(defaultValue = "false") boolean isProduction) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("transBeginTime", request.getTransBeginTime());
            params.put("transEndTime", request.getTransEndTime());
            if (request.getMchntNo() != null) {
                params.put("mchntNo", request.getMchntNo());
            }
            if (request.getGroupCode() != null) {
                params.put("groupCode", request.getGroupCode());
            }
            params.put("queryType", request.getQueryType());
            params.put("belongBranch", request.getBelongBranch());
            
            Map<String, Object> result = chinaUMSWFService.queryTransSummary(params, authorization, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("实时交易汇总查询接口调用失败", e);
            return R.fail("实时交易汇总查询接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 对账汇总查询接口
     * @param request 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    @PostMapping("/accountSummary")
    @ApiOperation("对账汇总查询接口")
    public R accountSummary(@RequestBody AccountSummaryRequest request,
                            @RequestHeader("Authorization") String authorization,
                            @RequestParam(defaultValue = "false") boolean isProduction) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("beginDate", request.getBeginDate());
            params.put("endDate", request.getEndDate());
            params.put("mchntNo", request.getMchntNo());
            params.put("belongBranch", request.getBelongBranch());
            
            Map<String, Object> result = chinaUMSWFService.accountSummary(params, authorization, isProduction);
            return R.data(result);
        } catch (Exception e) {
            log.error("对账汇总查询接口调用失败", e);
            return R.fail("对账汇总查询接口调用失败：" + e.getMessage());
        }
    }
}